package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.common.R;
import com.example.pojo.entity.WmNews;
import com.example.service.NewsService;
import com.example.service.impl.NewsServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import static com.example.utils.JsonChangeObject.JsonObject;

@WebServlet("/update")
public class UpdateNewsServlet extends HttpServlet {


    NewsService newsService = new NewsServiceImpl();

    /**
     * 修改个人作品
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        String jsonStr = JsonObject(req);
        WmNews wmNews = JSON.parseObject(jsonStr, WmNews.class);
        try {
            R data = newsService.updatePersonWork(wmNews);
            //返回结果
            resp.setContentType("text/html");
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().write(JSON.toJSONString(data));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
